using IceDog.WebApi.ConventionalRoute.DBData;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;
using System;
using System.IO;
using System.Reflection;

namespace IceDog.WebApi.ConventionalRoute
{
    /// <summary>
    /// 
    /// </summary>
    public class Startup
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="configuration"></param>
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }
        /// <summary>
        /// 
        /// </summary>
        public IConfiguration Configuration { get; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="services"></param>
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            //内存数据库，用于测试很方便
            services.AddDbContext<ShopContext>(options => options.UseInMemoryDatabase("Shop"));
            services.AddControllers();
            ServicesAddSwaggerGen(services);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="app"></param>
        /// <param name="env"></param>
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
            AppUseSwagger(app);
        }

        #region ConfigureServices 详细的添加内容
        /// <summary>
        /// 添加swagger gen
        /// </summary>
        /// <param name="services"></param>
        private void ServicesAddSwaggerGen(IServiceCollection services)
        {
            // Register the Swagger generator, defining one or more Swagger documents
            services.AddSwaggerGen(options =>
            {
                var title = "CR API";
                var license = new OpenApiLicense()
                {
                    Name = "GPL V3",
                    Url = new Uri("https://www.example.com/gpl_v3.html")
                };
                var contact = new OpenApiContact()
                {
                    Name = "DHclly",
                    Email = "335121817@qq.com",
                    Url = new Uri("https://www.example.com/contact_me.html")
                };
                var description = "this project use for test Conventional Route";

                options.SwaggerDoc("version_1", new OpenApiInfo
                {

                    Title = title,
                    Version = "v1.0",
                    License = license,
                    Contact = contact,
                    Description = description
                });

                // Set the comments path for the Swagger JSON and UI.
                var xmlFile = $"{Assembly.GetEntryAssembly().GetName().Name}.xml";
                var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                if (File.Exists(xmlPath))
                {
                    options.IncludeXmlComments(xmlPath);
                }

                options.DescribeAllParametersInCamelCase();//所有参数变量用camelCase
            });
        }
        #endregion

        #region Configure 详细的添加内容
        /// <summary>
        /// app use swagger
        /// </summary>
        /// <param name="app"></param>
        private void AppUseSwagger(IApplicationBuilder app)
        {
            // Enable middleware to serve generated Swagger as a JSON endpoint.
            app.UseSwagger(options=> {
                options.RouteTemplate = "api-docs/{documentName}/swagger.json";
            });

            // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), specifying the Swagger JSON endpoint.
            app.UseSwaggerUI(options =>
            {
                options.SwaggerEndpoint("/api-docs/version_1/swagger.json", "CR API Version 1.0");
                options.RoutePrefix = "api-docs";
            });
        }
        #endregion
    }
}
